
define(['jquery', 'bootstrap', 'userend', 'table', 'form', 'raty'], function ($, undefined, Userend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/goods/myappraise/index',
                    add_url: 'mall/goods/myappraise/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'mall/goods/myappraise/multi',
                    table: 'mall_goods_appraise',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), visible:false},
                        {field: 'shop_id', title: __('Shop_id'), visible:false},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'goods_id_text', title: __('Goods_id')},
                        {field: 'goodsspec_id', title: __('goodsspec_id'), visible:false},
                        {field: 'user_id', title: __('User_id'), visible:false},
                        {field: 'goodsraty', title: __('Goodsscore'), formatter: Controller.api.formatter.score},
                        {field: 'serviceraty', title: __('Servicescore'), formatter: Controller.api.formatter.score},
                        {field: 'timeraty', title: __('Timescore'), formatter: Controller.api.formatter.score},
                        {field: 'showis', title: __('Showis'), formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.flag, visible:false},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[{
                                name: 'appraise',
                                text: __('Appraise'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-pencile',
                                visible: function( row){
                                    return  row.goodsraty==0 && row.serviceraty==0 && row.timeraty==0;
                                },
                                url: 'mall/goods/myappraise/edit',
                                refresh: $(".btn-refresh").trigger("click")
                            }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                score: function (value, row, index) {
                    var stars=[];
                    for(i=1;i<=5;i++){
                        if(i<=value){
                            stars[i]='<i class="fa fa-star"></i>'
                        }else{
                            stars[i]='<i class="fa fa-star-o"></i>'
                        }
                    }
                    return '<div class="rating">'+stars.join("")+'</div>';
                },
            }
        }
    };
    return Controller;
});